package org.batatasframework.support.event.internal;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.batatasframework.support.database.TableInfo;
import org.batatasframework.support.database.sql.TableInstance;
import org.batatasframework.support.database.sql.field.SelectFragment;
import org.batatasframework.support.database.sql.spi.TableCriterion;
import org.batatasframework.support.definition.ConditionDefinition;
import org.batatasframework.support.definition.FieldDefinition;
import org.batatasframework.support.definition.JoinDefinition;
import org.batatasframework.support.definition.QueryDefinition;
import org.batatasframework.support.definition.SortDefinition;
import org.batatasframework.support.definition.TableDefinition;
import org.batatasframework.support.event.SearchableHttpRequest;

import cn.bonoon.kernel.support.IOperator;
import cn.bonoon.kernel.util.StringHelper;

public class DefaultSearchableHttpRequest extends AbstractSearchableRequest<TableDefinition> implements SearchableHttpRequest{
	
	private final String DISPLAY_FIELD_NAME = "displayField";
	private final String GROUP_FIELD_NAME = "groupfield";
	
	private final HttpServletRequest servletRequest;
//	private final TableDefinition tableDefinition;
	
	public DefaultSearchableHttpRequest(IOperator operator, TableDefinition tableDefinition, HttpServletRequest servletRequest){
		super(operator);
//		this.tableDefinition = tableDefinition;
		this.servletRequest = servletRequest;
		this.sourceTableMetadata = tableDefinition;
	}
	
	@Override
	public void resolve(TableInstance tableInstance, TableCriterion tableCriterion) throws Exception {
		
		String[] displays = servletRequest.getParameterValues(DISPLAY_FIELD_NAME);
		
		if(null == displays || displays.length == 0){
			 throw new Exception("没有选择需要显示的字段！");
			
		}
		
		List<JoinDefinition> mustJoinTables = sourceTableMetadata.getMustJoinTables();
//		List<FieldDefinition> defaultFeilds = sourceTableMetadata.getMustDisplayField();

		for(JoinDefinition jd : mustJoinTables){
			jd.loadJoinTable(tableCriterion, true);
		}
		
		String groupField = servletRequest.getParameter(GROUP_FIELD_NAME);
		SelectFragment selectFragment = null;
		boolean asStatisitics = false;
		
		if(StringHelper.isNotEmpty(groupField)){
			FieldDefinition fieldDefinition = sourceTableMetadata.getDefinition(groupField);
			selectFragment = fieldDefinition.buildFragment(tableCriterion);
			asStatisitics = "1".equals(servletRequest.getParameter("asstatisitics"));
		}else{
			
			for(FieldDefinition fieldDefinition : sourceTableMetadata.getColumns()){
				if(fieldDefinition.isMustDiaplay()){
					fieldDefinition.buildFragment(tableCriterion);
				}
			}
		
		}
		
		inputStream = buildInputStream(selectFragment, asStatisitics);
		
		for(String display : displays){
			FieldDefinition fieldDefinition = sourceTableMetadata.getDefinition(display);
			fieldDefinition.buildFragment(tableCriterion);
		}
		
		String[] queries = servletRequest.getParameterValues("queryIndex");
		if(null != queries){
			for(String ci : queries){
				QueryDefinition queryDefine = sourceTableMetadata.query(getString("field_", ci));
				if(null != queryDefine){
//					Opt opt = Opt.valueOf(getString("opt_", ci));
//					String[] values = getStringArray("value_", ci);
//					
//					tableCriterion.newQuery(queryDefine, opt, values);
					tableCriterion.newQuery(queryDefine, this, ci);
				}
			}
		}
		
		//TODO 这里需要添加默认定义的查询条件或排序等
		for(ConditionDefinition condition : sourceTableMetadata.getConditions()){
			condition.buildQuery(tableCriterion);
		}
		
		for(SortDefinition sort : sourceTableMetadata.getSorts()){
			sort.buildSort(tableCriterion);
		}
	}

	@Override
	public TableInfo getTableInfo() {
		return sourceTableMetadata.getTableInfo();
	}
	
	@Override
	public Long getLong(String arg) {
		return StringHelper.toLong(servletRequest.getParameter(arg));
	}
	
	@Override
	public Long getLong(String arg0, String arg1) {
		return StringHelper.toLong(servletRequest.getParameter(arg0 + arg1));
	}

	@Override
	public String getString(String arg) {
		return servletRequest.getParameter(arg);
	}

	@Override
	public String getString(String arg0, String arg1) {
		return servletRequest.getParameter(arg0 + arg1);
	}

	@Override
	public String[] getStringArray(String arg) {
		return servletRequest.getParameterValues(arg);
	}

	@Override
	public String[] getStringArray(String arg0, String arg1) {
		return servletRequest.getParameterValues(arg0 + arg1);
	}

}
